'use strict';

import Base from './base.js';
import request from 'request';

export default class extends Base {
    /**
     * index action
     * @return {Promise} []
     */
    async indexAction() {
        //  await this.subjectlistAction();
        /*var test = await this.subjectlistAction();
        console.log('test', test)*/
        //auto render template file index_index.html
        this.assign("title", 'TOP10 学科');
        //设置CSRF值
        let csrf = await this.session("__CSRF__");
        this.assign("csrf", csrf);
        return this.display();
    }


    /* 获取 getListData 接口数据 */
    async subjectlistAction() {
        let getListData = () => {
            let fn = think.promisify(request.get);
            return fn({
                url: 'http://192.168.100.15:18080/med/api/subject/list',
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) Chrome/47.0.2526.111 Safari/537.36'
                }
            });
        }

        getListData().then(response => {
            let body = JSON.parse(response.body);
            if (body.code == 200) {
                this.json(body);
            }
        }).catch(function(err) {
            console.error(err.stack);
        });

        //debug
        if (false) {
            var test = await this.model('user').select();
            console.log('test>>>', test)
            return this.json({
                status: 0,
                errno: 1,
                //errmsg: "用户名或密码错误!"
                errmsg: test
            });
        }
    }

    /*获取单个信息*/
    //http://192.168.100.15:18080/med/api/subject/single/list?cid=R395&__CSRF__=IhlLE2_O5dxiLzh8fruuhWWjGh1d3kYE
    async singlelistAction() {
        var getSingleListData = () => {
            let fn = think.promisify(request.get);
            let cid = this.get('cid');
            return fn({
                url: 'http://192.168.100.15:18080/med/api/subject/single/list?cid=' + cid,
                //  url: 'http://192.168.100.15:18080/med/api/subject/single/list',
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) Chrome/47.0.2526.111 Safari/537.36'
                }
            });
        };

        getSingleListData().then(response => {
            let body = JSON.parse(response.body);
            if (body.code == 200) {
                this.json(body);
            }
        }).catch(function(err) {
            console.error(err.stack);
        });

    }

    //list/keyword
    async keywordAction() {
        this.assign("title", 'TOP10 学科');
        //设置CSRF值
        let csrf = await this.session("__CSRF__");
        this.assign("csrf", csrf);
        return this.display();
    }

    /* 获取 keyword 接口数据 */
    async keywordlistAction() {
        let getListData = () => {
            let fn = think.promisify(request.get);
            let id = this.get('id');
            return fn({
                url: 'http://192.168.100.15:18080/med/api/subject/word/detail?id=' + id,
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) Chrome/47.0.2526.111 Safari/537.36'
                }
            });
        }

        getListData().then(response => {
            let body = JSON.parse(response.body);
            if (body.code == 200) {
                this.json(body);
            }
        }).catch(function(err) {
            console.error(err.stack);
        });

    }
}
